package com.dongdongshop.Service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.dongdongshop.mapper.TbItemCatMapper;
import com.dongdongshop.pojo.TbItemCat;
import com.dongdongshop.service.ItemCatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@org.springframework.stereotype.Service
public class ItemCatServiceImpl implements ItemCatService {

    @Autowired
    private TbItemCatMapper tbItemCatMapper;

    @Override
    public List<TbItemCat> itemCatList(Long parentId) {
        return tbItemCatMapper.itemCatList(parentId);
    }

    @Override
    public Integer save(TbItemCat tbItemCat) {
        return tbItemCatMapper.insert(tbItemCat);
    }

    @Override
    public TbItemCat updateById(Long id) {
        return tbItemCatMapper.selectByPrimaryKey(id);
    }

    @Override
    public Integer updateInfo(TbItemCat tbItemCat) {
        return tbItemCatMapper.updateByPrimaryKey(tbItemCat);
    }

    @Override
    @Transactional
    public Integer deleteCarById(Long[] id) {
        //递归 查找子id
        for (int i = 0;i < id.length;i++){
            getList(id[i]);
        }
        return tbItemCatMapper.deleteCarById(id);
    }

    public void getList(Long id){
        //查询所有子id
       List<Long> parentId = tbItemCatMapper.getParentId(id);
           //删除继续递归调用
           tbItemCatMapper.deleteByPrimaryKey(id);
           for (Long pid : parentId) {
               getList(pid);
           }
    }
}
